package com.mondo.financialwallet.data.repository;

import com.mondo.financialwallet.data.entity.mapper.WalletDataEntityMapper;
import com.mondo.financialwallet.data.repository.datasource.RemoteWalletDataStore;
import com.mondo.financialwallet.data.repository.datasource.WalletDataStore;
import com.mondo.financialwallet.domain.interactor.GetTransactionRecordList;
import com.mondo.financialwallet.domain.interactor.PostTokenTransfer;
import com.mondo.financialwallet.domain.model.Response;
import com.mondo.financialwallet.domain.model.Token;
import com.mondo.financialwallet.domain.model.TransactionRecordList;
import com.mondo.financialwallet.domain.model.WalletAsset;
import com.mondo.financialwallet.domain.repository.WalletRepository;

import java.util.List;

import io.reactivex.Observable;

public class WalletDataRepository implements WalletRepository {

    private final WalletDataEntityMapper mapper;

    private final WalletDataStore walletDataStore;

    public WalletDataRepository() {
        this.walletDataStore = new RemoteWalletDataStore();
        mapper = new WalletDataEntityMapper();
    }

    @Override
    public Observable<Response<List<Token>>> myTokenList() {
        return walletDataStore.myTokenList().map(mapper::transform);
    }

    @Override
    public Observable<Response<TransactionRecordList>> transactionRecordList(
            GetTransactionRecordList.Params params) {
        return walletDataStore
                .transactionRecordList(params.tokenId, params.page, params.type)
                .map(mapper::transform);
    }

    @Override
    public Observable<Response<Token>> generateTokenAddress(String id) {
        return walletDataStore.generateTokenAddress(id).map(mapper::transform);
    }

    @Override
    public Observable<Response<WalletAsset>> walletAsset() {
        return walletDataStore.walletAsset().map(mapper::transform);
    }

    @Override
    public Observable<Response<WalletAsset>> walletAvailableBalance() {
        return walletDataStore.walletAvailableBalance().map(mapper::transform);
    }

    @Override
    public Observable<Response> transfer(PostTokenTransfer.Params params) {
        return walletDataStore
                .transfer(params.tokenId, params.amount, params.address, params.remark)
                .map(mapper::transform);
    }
}
